package com.tvunetworks.center.device.service;


import com.github.pagehelper.PageInfo;
import com.tvunetworks.center.common.model.ExtendExternal;
import com.tvunetworks.center.common.model.LoginUser;
import com.tvunetworks.center.common.model.PreloadParam;
import com.tvunetworks.center.common.model.SearchDeviceParam;
import com.tvunetworks.center.common.model.param.search.SlugInfo;
import com.tvunetworks.center.common.util.Result;
import com.tvunetworks.center.device.model.MyDevice;
import com.tvunetworks.center.device.model.json.DeviceIpPort;
import com.tvunetworks.center.device.model.param.AddExtToReceiverParam;
import com.tvunetworks.center.device.model.param.DevicePlatformParam;
import com.tvunetworks.center.device.model.vo.NotConnectR;
import com.tvunetworks.center.device.model.vo.SlotVo;
import com.tvunetworks.center.gs.model.Device;

import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * @author lebronchen
 * @program: tvucc-instance
 * @create: 2019-02-28 16:53
 **/
public interface DeviceService {
    /**
     * @param peerId peerId
     * @return getMemDevice
     */
    Device getMemDevice(String peerId);


    /**
     * 根据批量的peerId,获取其中的Rid
     *
     * @param rid rid
     * @return List
     */
    List<String> getReceiverByPeerId(List<String> rid);

    /**
     * @param peerId peerId
     * @return MyDevice
     */
    MyDevice getDBDevice(String peerId);

    /**
     * @param peerId peerId
     * @return Device
     */
    Device getDevice4DB(String peerId);

    /**
     * @param device device
     */
    void updateDevice(MyDevice device);

    /**
     * @param peerIds peerIds
     * @return List
     */
    List<Device> listMemDevice(Collection<String> peerIds);

    /**
     * 获取device信息从缓存中
     *
     * @param peerId peerId
     * @return Device
     */
    Device getCurrentDevice(String peerId);

    /**
     * @param peerIds peerIds
     * @return List
     */
    List<Device> listCurrentDevice(Collection<String> peerIds);

    /**
     * @param peerIds peerIds
     * @return Map
     */
    Map<String, String> listDeviceMetadata(Collection<String> peerIds);

    /**
     * 修改Anywhere名字
     *
     * @param peerId  peerId
     * @param name    name
     * @param session session
     * @return Result
     */
    Result<String> updateAnywhereName(String peerId, String name, String session);

    /**
     * @param peerId  peerId
     * @param name    name
     * @param type    type
     * @param session session
     * @return Result
     */
    Result<String> updateDeviceName(String peerId, String name, String type, String session);

    /**
     * @param device device
     */
    void setExtToMemcache(Device device);

    /**
     * @param peerId peerId
     */
    void deleteExtFromMemcache(String peerId);

    /**
     * @param peerId peerId
     * @return String
     * @throws IOException 编译异常
     */
    String queryDetail(String peerId) throws IOException;

    /**
     * @param peerId peerId
     * @return String
     * @throws IOException 编译异常
     */
    String queryBatteryById(String peerId) throws IOException;

    /**
     * @param db       db
     * @param matrixId matrixId
     * @return String
     */
    String getSwitchType(String db, String matrixId);

    /**
     * @param param param
     * @return List
     */
    List<Device> listAllDevices(SearchDeviceParam param);

    /**
     * @param peerId peerId
     * @return PreloadParam
     */
    PreloadParam getRPreloadMessage(String peerId);

    /**
     * @param rid rid
     */
    void preloadExtXToR(String rid);

    /**
     * @param rid rid
     */
    void preloadEventExtToR(String rid);

    /**
     * Check device status live or not
     *
     * @param peerId peerId
     * @return true live, false not live
     */
    boolean isLive(String peerId);

    /**
     * @param oldPeerId oldPeerId
     * @param newPeerId newPeerId
     * @return Result
     */
    // ********************* Device Copy Start *********************
    Result<String> isDeviceExist(String oldPeerId, String newPeerId);

    /**
     * @param oldPeerId oldPeerId
     * @param newPeerId newPeerId
     * @param taskId    taskId
     * @return Result
     */
    Result<String> checkCopy(String oldPeerId, String newPeerId, String taskId);

    /**
     * 复制device
     *
     * @param oldPeerId oldPeerId
     * @param newPeerId newPeerId
     * @param taskId    taskId
     * @return Result
     */
    Result<String> copyDevice(String oldPeerId, String newPeerId, String taskId);

    /**
     * 删除device
     *
     * @param peerId peerId
     * @param taskId taskId
     * @return Result
     */
    Result<String> deleteDevice(String peerId, String taskId);


    // ********************* Device Copy End *********************

    /**
     * @param ips     ips
     * @param peerIds peerIds
     * @param mapType mapType
     * @return Result
     */
    Result<String> listDeviceLocationByMultiIpOrPeerId(String ips, String peerIds, String mapType);

    /**
     * @param peerIds peerIds
     * @return Result
     */
    Result<String> listGPSByMultiPeerId(String peerIds);

    /**
     * @param peerId peerId
     * @return Result
     * @throws Exception 编译异常
     */
    Result<String> getMetadata(String peerId) throws Exception;

    /**
     * @param peerId peerId
     * @return Result
     * @throws Exception 编译异常
     */
    Result<String> getMetadataList(String peerId) throws Exception;

    /**
     * @param key   key
     * @param value value
     */
    void setToTpdsMemcache(String key, String value);

    /**
     * @param peerId peerId
     * @return boolean
     */
    boolean doubleCheckDeviceOffline(String peerId);

    /**
     * @param peerId  peerId
     * @param name    name
     * @param session session
     */
    void globalUpdateDeviceName(String peerId, String name, String session);

    /**
     * @param device device
     * @return List
     */
    List<Device> listDeviceByExample(Device device);

    /**
     * @param rid rid
     * @return Result
     */
    Result<String> getRSourceTypeList(String rid);

    /**
     * @param searchDeviceParam searchDeviceParam
     * @return PageInfo
     */
    PageInfo<ExtendExternal> getGLinkList(SearchDeviceParam searchDeviceParam);

    /**
     * @param searchDeviceParam searchDeviceParam
     * @return PageInfo
     */
    PageInfo<ExtendExternal> getRTILList(SearchDeviceParam searchDeviceParam);

    /**
     * Receiver 上线，通知 token 服务，如果 R ip 发生变化，重新下发配对信息
     *
     * @param message message
     * @throws IOException 编译异常
     */
    void notifyTokenReceiverOnline(String message) throws IOException;

    /**
     * @param pageNum  pageNum
     * @param pageSize pageSize
     * @return PageInfo
     */
    PageInfo<NotConnectR> listNotConnectR(int pageNum, int pageSize);

    /**
     * @param peerId peerId
     */
    void initReceiverUploadParam(String peerId);

    /**
     * @param rid rid
     * @return List
     */
    List<Device> listReceiverGrid(String rid);

    /**
     * @param rids rids
     * @return Map
     */
    Map<String, List<Device>> listReceiversGrid(List<String> rids);

    /**
     * @param pids pids
     * @return List
     * @throws ExecutionException   编译异常
     * @throws InterruptedException 编译异常
     */
    List<DeviceIpPort> getDeviceIpPort(List<String> pids) throws ExecutionException, InterruptedException;

    /**
     * 获取指定R下WebR创建的Ext
     *
     * @param rid rid
     * @return List
     */
    List<Device> getExtFromWebR(String rid);

    /**
     * 获取指定R下WebR创建的LocalSDI
     *
     * @param rid rid
     * @return List
     */
    List<Device> getLocalSDIFromWebR(String rid);

    /**
     * 获取指定R下WebR创建的GLink
     *
     * @param rid rid
     * @return List
     */
    List<ExtendExternal> getGLinkFromWebR(String rid);

    /**
     * 获取所有Ip不为null的R
     *
     * @return List
     */
    List<Device> listReceiverByIpIsNotNull();

    /**
     * 查询pack的slotIp
     *
     * @param peerId pack的id
     * @return slotIp集合
     * @throws Exception exception
     */
    List<SlotVo> listLocalIp(String peerId) throws Exception;


    /**
     * @param list list
     * @return int
     * @throws Exception if has error
     * @author KevinLi
     * @date 12/7/2021
     */
    int synGridSlug(List<SlugInfo> list);


    /**
     * @author kevin
     * @param peerId peerId
     * @param slug slug
     * @return int 数量
     * @throws Exception if has error
     **/
    int updateMetadata(String peerId, String slug);


    /**
     * 根据peerId 修改platform
     *
     * @param device    参数
     * @param loginUser loginUser
     * @return Result
     */
    Result updatePlatform(DevicePlatformParam device, LoginUser loginUser);

    /**
     * 从内存中获取全量的T列表
     * @return List<String>
     */
    List<Object> getPST();

    /**
     * 从内存中获取全量的R列表
     * @return List<String>
     */
    List<Object> getPSR();

    /**
     * 添加ext到receiver上,全量下发
     * @param param param
     * @return Result
     */
    Result addExtToReceiver(AddExtToReceiverParam param);

}
